# -*- coding:utf-8 -*-
'''
对word文档进行转换，保存在txt文件；
在转换过程中对中文大写等转换成罗马数字
'''

import docx2txt
import os
import re
import pickle
from collections import OrderedDict
from demo.config import customInfoDir,standardNameflag
from dirHandler import makeDir
from fileHandler import FileOperation
from errorRecordHandler import recordError
from blockHandler import step2Handler,step3Handler,step4Handler,step5Handler,stepCompanyNameHandler,stepGovNameHandler,stepGetiNameHandler,companyNameAddSpace

# pkl_file = open(regionFile, 'rb')
# regionInfo = pickle.load(pkl_file)
# pkl_file.close()
class Word2Txt(object):
    def readWord(self,file):
        '''
        :param file:
        :return:返回word文档中的文本信息，并且对文本信息进行预处理，按行进行分隔
        '''
        # 在对word文档的内容进行处理时实现分行
        text = docx2txt.process(file)   #step-1
        text=text.replace("―","-")
        res=[]
        for each in text.split("\n"):   #按照显式进行分行
            lineTxt=each.strip()
            if len(lineTxt)!=0:    #删除空行   step-2
                res.append(lineTxt)
            else:
                continue
        return res

    def countMark(self,context):
        # 后续需要添加表示字符识别
        count=0
        for item in context:
            if self.isIncludetarget(item,u'地址') or self.isIncludetarget(item,u'邮箱') or self.isIncludetarget(item,u'网址') or self.isIncludetarget(item,u'QQ') or self.isIncludetarget(item,u'手机') or self.isIncludetarget(item,u'座机') or self.isIncludetarget(item,u'微信') or self.isIncludetarget(item,u'联系人'):
                count+=1
            else:
                continue
        # 控制一个模块有多少个标准名称
        if count >=2:
            return True
        else:
            return False
    def isIncludetarget(self,string,target):
        if target in string:
            return True
        else:
            return False

    def word2CustomInfoSegmen(self,dirpath,inputfile,parentDirMd5,catalog):
        context=self.readWord(dirpath+inputfile)
        self.customFenkuai(context,catalog,parentDirMd5)

    def customFenkuai(self,content,catalog,parentDirMd5):
        content=u'\n'.join(content)

        # 使用新方法进行处理
        # 判断是否有无效分隔符，如果有九输出错误记录，同时删除
        for k,v in catalog.items():
            if v not in content:
                recordError(parentDirMd5,1,v)
                del catalog[k]
            else:
                continue
        catalogTemp=OrderedDict(zip(catalog.values(),catalog.keys()))
        # 获取自定义分隔符列表
        catalogList=catalog.values()
        catalogNum=len(catalogList)
        if catalogNum==1 and catalogList[0] in content:
            fileTargetMd5=catalogTemp[catalogList[0]]
            makeDir(customInfoDir+"\\"+parentDirMd5,fileTargetMd5)
            fileobj=FileOperation(customInfoDir+"\\"+parentDirMd5+"\\"+fileTargetMd5,"")
            fh1=fileobj.fileCreate("fullInfo")
            fh1.write(content)
            fileobj.fileClose(fh1)
        else:
            for i in range(0,catalogNum-1):
                # 分割后保存文件的目录名，原始名称
                fileTarget=catalogList[i]
                # 用于分割文件的自定义目录名称
                splitTarget=catalogList[i+1]
                #处理分割文件
                res=content.split(splitTarget,1)
                text=res[0]
                if i==catalogNum-2:
                    if len(res)==2 and text !='' and fileTarget in text:
                        fileTargetMd5=catalogTemp[fileTarget]
                        splitTargetMd5=catalogTemp[splitTarget]

                        makeDir(customInfoDir+"\\"+parentDirMd5,fileTargetMd5)
                        fileobj=FileOperation(customInfoDir+"\\"+parentDirMd5+"\\"+fileTargetMd5,"")
                        fh1=fileobj.fileCreate("fullInfo")
                        fh1.write(text)
                        fileobj.fileClose(fh1)

                        makeDir(customInfoDir+"\\"+parentDirMd5,splitTargetMd5)
                        fileobj=FileOperation(customInfoDir+"\\"+parentDirMd5+"\\"+splitTargetMd5,"")
                        fh1=fileobj.fileCreate("fullInfo")
                        fh1.write(splitTarget+"\n"+res[-1])
                        fileobj.fileClose(fh1)
                else:
                    if len(res)==2 and text !='' and fileTarget in text:
                        #表示res[0]是一个分块
                        fileTargetMd5=catalogTemp[fileTarget]
                        #创建文件夹和内容文件
                        makeDir(customInfoDir+"\\"+parentDirMd5,fileTargetMd5)
                        fileobj=FileOperation(customInfoDir+"\\"+parentDirMd5+"\\"+fileTargetMd5,"")
                        fh1=fileobj.fileCreate("fullInfo")
                        fh1.write(text)
                        fileobj.fileClose(fh1)
                        content=splitTarget+"\n"+res[-1]
                        continue
        #处理分片信息
        self.customFenPian(parentDirMd5)
    def customFenPian(self,parentDirMd5):
        targetDir=customInfoDir+"\\"+parentDirMd5
        targetFenkuaiDir=os.listdir(targetDir)
        for oneFenkuai in targetFenkuaiDir:
            #读取分块内容
            try:
                with open(targetDir+"\\"+oneFenkuai+"\\"+"fullInfo.txt") as f:
                    content=f.read()
                    content=content.replace("*","&")
                content=unicode(content)
                targetContent=content
                text=step2Handler(content)
                # 1-2中间结果输出
                target1_2=targetDir+"\\"+oneFenkuai+"\\"+"step1-2.txt"
                self.middleResultOutput(target1_2,text)

                text=step3Handler(text)
                # 1-3中间结果输出
                target1_3=targetDir+"\\"+oneFenkuai+"\\"+"step1-3.txt"
                self.middleResultOutput(target1_3,text)

                text=step4Handler(text)
                # 1-4中间结果输出
                target1_4=targetDir+"\\"+oneFenkuai+"\\"+"step1-4.txt"
                self.middleResultOutput(target1_4,text)

                text=step5Handler(text)
                # 1-5中间结果输出
                target1_5=targetDir+"\\"+oneFenkuai+"\\"+"step1-5.txt"
                self.middleResultOutput(target1_5,text)
            except:
                recordError(parentDirMd5,2,targetContent)
                continue
            #对公司名称进行处理时所用到经过文本处理后的原始内容信息
            fullText=text
            # 进入分片正式流程
            qqContentList=[]
            weichatContentList=[]
            emailContentList=[]
            addressContentList=[]
            phoneContentList=[]
            telContentList=[]
            urlContentList=[]
            specialContentList=[]

            # ---------------捕获分片异常-----------------------
            try:
                temp=re.findall(standardNameflag,text)
                temp.reverse()
                for each in temp:
                    ind=text.rindex(each)

                    content=text[ind:]

                    text=text[0:ind]
                    #地址|邮箱|网址|QQ||手机|座机|微信|特例名
                    if each ==u'地址':
                        addressContentList.append(content)
                    elif each ==u'邮箱':
                        emailContentList.append(content)
                    elif each ==u'网址':
                        urlContentList.append(content)
                    elif each ==u'QQ':
                        qqContentList.append(content)
                    elif each ==u'手机':
                        phoneContentList.append(content)
                    elif each ==u'座机':
                        telContentList.append(content)
                    elif each ==u'微信':
                        weichatContentList.append(content)
                    else:
                        specialContentList.append(content)

                # 创建文件
                fileObj=FileOperation(targetDir+"\\"+oneFenkuai,oneFenkuai)


            except:
                recordError(parentDirMd5,2,targetContent)
                continue

            # 对公司名称进行分单元
            try:
                self.customCompanyUnit(fullText,fileObj)
            except:
                recordError(parentDirMd5,3,targetContent)
                continue
            # --------------------为了保持写入一致性------------------------
            qqFile=fileObj.fileCreate('qq')
            qqContentList.reverse()
            qqFile.write("\n".join(qqContentList))
            weichatFile=fileObj.fileCreate('weichat')
            weichatContentList.reverse()
            weichatFile.write("\n".join(weichatContentList))
            emailFile=fileObj.fileCreate('email')
            emailContentList.reverse()
            emailFile.write("\n".join(emailContentList))
            addressFile=fileObj.fileCreate('address')
            addressContentList.reverse()
            addressFile.write("\n".join(addressContentList))
            phoneFile=fileObj.fileCreate('phone')
            phoneContentList.reverse()
            phoneFile.write('\n'.join(phoneContentList))
            telFile=fileObj.fileCreate('telphone')
            telContentList.reverse()
            telFile.write('\n'.join(telContentList))
            telFile.write("\n")

            urlFile=fileObj.fileCreate('url')
            urlContentList.reverse()
            urlFile.write('\n'.join(urlContentList))
            specialFile=fileObj.fileCreate('special')
            specialContentList.reverse()
            specialFile.write('\n'.join(specialContentList))

            #文件关闭操作
            fileObj.fileClose(qqFile)
            fileObj.fileClose(addressFile)
            fileObj.fileClose(weichatFile)
            fileObj.fileClose(emailFile)
            fileObj.fileClose(phoneFile)
            fileObj.fileClose(telFile)
            fileObj.fileClose(urlFile)
            fileObj.fileClose(specialFile)
    def customCompanyUnit(self,content,fileobj):


        content=companyNameAddSpace(content)

        companyNameFile=fileobj.fileCreate('companyName')
        for line in content.split('\n'):
            line=line.strip().strip('"')
            if len(line)==0:
                continue
            else:

                if u'公司' in line and line.count(u'公司')>1:
                    # index=line.find(u"公司")
                    index=line.rfind(u"公司")
                    long=len(u"公司")
                    tmp=[line[:index+long],line[index+long:]]
                    if len(tmp[-1])<8:
                        tmp=[u"".join(tmp)]
                    for each in tmp:
                        # print "-------------------------"
                        # print each

                        temp=stepCompanyNameHandler(each) or stepGovNameHandler(each) or stepGetiNameHandler(each)
                        # print temp
                        # print "-------------------------"
                        if temp:
                            companyNameFile.write(each)
                            companyNameFile.write("\n")
                        else:
                            continue
                else:

                    temp=stepCompanyNameHandler(line) or stepGovNameHandler(line) or stepGetiNameHandler(line)
                    # print "==========================="
                    # print line
                    # print temp
                    # print "==========================="
                    if temp:
                        companyNameFile.write(line)
                        companyNameFile.write("\n")
                    else:
                        continue
        fileobj.fileClose(companyNameFile)


    def middleResultOutput(self,file,text):
        with open(file,'w') as f:
            f.write(text)
